#!/bin/sh

#set -x
set -e

file_name=$(date "+%Y-%m-%d")

if [ ! -d "/$file_name/"]; then
	echo "dir was exist and remove it..."
	rm -rf ./$file_name
fi

echo "make dir..."
mkdir /home/warhistory/$file_name

echo "backup war to my dir..."
cp /home/tomcat_prod/webapps/scdp.war ./$file_name

echo "stop tomcat..."
cd /home/tomcat_prod/bin
sh catalina.sh stop

echo "query tomcate status"
while true
do
	process=`ps aux | grep java | grep -v grep`;

	if [ "$process" != "" ]; then
			sleep 1;
			echo "process exist";
	else
			echo "process close";
			break;
	fi
done

echo "copy new war to tomcat..."
cd /home/tomcat_prod/webapps
rm -rf scdp.war
rm -rf scdp
cp /home/warhistory/scdp.war ./

echo "start tomcat..."
cd /home/tomcat_prod/bin
sh catalina.sh start

echo "show log..."
tail -f /home/tomcat_prod/logs/catalina.out


